﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using QbservableCore.Serialization.Helpers;
using System.Runtime.Serialization;

namespace QbservableCore.Serialization
{
    [DataContract]
    public class ElementInitNode
    {
        [DataMember]
        public SerializableMethodBase MethodRepresentation { get; set; }

        [DataMember]
        public string AddMethodName { get; set; }

        [DataMember]
        public ExpRepresentationBase Argument { get; set; }

        public ElementInit ToElementInit(ExepressionBuilderCache exepressionBuilderCache)
        {
            //var addMethod = (MethodInfo)MethodRepresentation.Deserialize();
            var addMethod = (MethodInfo)MethodRepresentation.Method;
            return Expression.ElementInit(addMethod, Argument.ToExpression(exepressionBuilderCache));
        }

        public override string ToString()
        {
            return string.Format("{0}({1})", AddMethodName, Argument);
        }

        public void TraverseChildren(Func<ExpRepresentationBase, ExpRepresentationBase> action)
        {
            Argument = Argument.Traverse(action);
        }
    }
}
